module ActsCrummy #:nodoc:
  # This module is mixed into ActiveRecord::Base to provide methods to help define
  # the direct hierarcy in the model structure of a project, so that breadcrumbs
  # can be rendered dynamically from it.

  module HasParent

    # the self.included/class << base trick is all for the sake
    # seemingly neceesary class variable @@parent_label
    def self.included(base) #:nodoc:

      # only apply this to inheriting classes, not to the parent itself
      # (probably ActiveRecord::Base)
      def base.inherited(subclass)
        super if defined? super

        class << subclass
          def is_child_to lbl #:doc:
            if method_defined? :parent
              raise ArgumentError, 'Only one parent can be defined for a given model.'
            end

            # this way we don't add the parent method unless
            # it's actually going to be used.  if we just added
            # to the class outright it may mess with the model
            # definition.
            send(:define_method, :parent) do
              send(lbl)
            end
          end
        end
      end
    end
  end
end
